﻿Imports System.Text
Imports System.Xml

Public Class InsertDatPersonalCommand
    Inherits PersonalCommand

    Private mSbDat As StringBuilder
    Private mDocDict As XmlDocument
    'Private sbTruncados As New StringBuilder()
    Dim listaTruncados = New List(Of Mensaje)

    Public Sub New(ByRef sbDat As StringBuilder, ByRef docDict As XmlDocument)
        mSbDat = sbDat
        mDocDict = docDict
    End Sub

    Public Overrides Sub procesar(ByVal p As Personal)
        Dim sbTruncadosLinea As New StringBuilder

        Dim sbLinea As New StringBuilder()
        sbLinea.Append(getValor("P_CODPER", p.dni, sbTruncadosLinea))
        sbLinea.Append(getValor("P_NOMBRE", p.nombre & " " & p.ap_pat & " " & p.ap_mat, sbTruncadosLinea))
        sbLinea.Append(getValor("P_ODEI", p.odei, sbTruncadosLinea))
        Dim cod As Integer = Integer.Parse(p.cargo.codigo)

        'Debido a que Personal.dcf del 2014 tiene cargo de longitud 1,
        'entonces solo se consideran cargos del 1 al 9
        If (cod < 10) Then
            p.cargo.codigo = cod & ""
        Else
            p.cargo.codigo = ""
            p.cargo.nombre = ""
        End If

        sbLinea.Append(getValor("P_CARGO", p.cargo.codigo, sbTruncadosLinea))
        sbLinea.Append(getValor("P_NOMCAR", p.cargo.nombre, sbTruncadosLinea))
        sbLinea.Append(getValor("P_DPTO", p.dpto, sbTruncadosLinea))
        'sbLinea.Append(getValor("P_ID", p.dni))
        'sbLinea.Append(getValor("P_SUPNAC", p.dni))
        mSbDat.AppendLine(sbLinea.ToString())

        If sbTruncadosLinea.Length > 0 Then
            'sbTruncados.AppendLine(sbTruncadosLinea.ToString & "(Para P_CODPER=" & p.dni & ")")
            Dim m As New Mensaje
            'm.linea = "P_CODPER=" & p.dni
            m.linea = p.dni
            m.descripcion = sbTruncadosLinea.ToString
            listaTruncados.add(m)
        End If

    End Sub

    Private Function getValor(ByVal nombreItem As String, ByVal valorItem As String, ByRef sbTruncadosLinea As StringBuilder) As String
        Dim nodo As XmlNode
        'nodo = mDocDict.SelectSingleNode("//Item")
        nodo = mDocDict.SelectSingleNode("//Item[@Name='" & nombreItem & "']")
        'Dim name As String = nodo.Attributes.GetNamedItem("Name").Value()
        'Dim start As String = nodo.Attributes.GetNamedItem("Start").Value()
        Dim len As String = nodo.Attributes.GetNamedItem("Len").Value()
        Dim a As Integer = valorItem.Length
        Dim b As Integer = Integer.Parse(len)
        Dim n As Integer = Math.Min(a, b)
        If a <= b Then
            Return valorItem.Substring(0, a) & StringUtil.repetir(" ", b - a)
        Else
            sbTruncadosLinea.Append(nombreItem & "(De " & a & " a " & b & " caracteres) ")
            Return valorItem.Substring(0, b)
        End If
    End Function

    Public Function getTruncados() As List(Of Mensaje)
        Return listaTruncados
    End Function

End Class
